import Vue from 'vue'
import Vuex from 'vuex'

import http from '@/common/http'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
        baseUrl:'https://wzlive.club/',
		userinfo:'',
        location:uni.getStorageSync('location') ? JSON.parse(uni.getStorageSync('location')):'',
        tag:false//通知
	},
	mutations: {
		SAVE_USERINFO(state, data) {
			state.userinfo = data || {}
		},
		SAVE_LOCATION(state, data) {
			state.location = data || {}
		},
        SAVE_TAG(state, data) {
        	state.tag = data ||false;
        }
	},
	actions: {
		wxLogin({commit},userInfo){
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						http.post('wxMPLogin', {
							code: loginRes.code
						}).then(res => {
							uni.setStorageSync('token', res.data.token);
							
							http.post('editInfo',{
								nickname:userInfo.nickName,
								avatar:userInfo.avatarUrl
							}).then(re=>{
								
								http.post('getUser').then(r => {
									commit('SAVE_USERINFO', r.data);
									resolve(r)
								})
							}).catch(err=>{
								reject(err)
							})
						})
					}
				});
			})
		},
        setLocation({commit},data){
            commit('SAVE_LOCATION',data)
        },
        getNoticeList({commit}){
            return new Promise((resolve, reject) => {
                http.post('noticeList').then(res=>{
                    console.log(res);
                    if(res.data.total > 0){
                        commit('SAVE_TAG',true);
                        console.log(this.state)
                    }else{
                        commit('SAVE_TAG',false);
                    }
                    
                    resolve(res);
                }).catch(err=>{
                    reject(err);
                })
            })
        }
	}
})

export default store
